<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<% String path=request.getContextPath(); %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<s:if test="actionMode=='update'">
	<title>Edit Patient Information</title>
</s:if>
<s:if test="actionMode=='create'">
	<title>Create Patient</title>
</s:if>
<link href="<s:url value="/style.css"/>" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
function addDoctorToTable()
{
		var tbl = document.getElementById('doctorTable');
		var lastRow = tbl.rows.length;
		// if there's no header row in the table, then iteration = lastRow + 1
		var row = tbl.insertRow(lastRow);
		// left cell
		var cellLeft = row.insertCell(0);
		/* var textNode = document.createTextNode(iteration); */

		// select cell
		/* var cellRightSel = row.insertCell(2); */
		var sel = document.createElement('select');
		sel.name = 'selectedReadOnlyDoctorIds';
		<s:iterator value="retrievedDoctors">
		sel.options.add(new Option("<s:property value='name'/>",
				"<s:property value='id'/>"));
		</s:iterator>
		/* cellRightSel.appendChild(sel); */
		cellLeft.appendChild(sel);
		// right cell
		var cellRight = row.insertCell(1);
		var link = document.createElement("a");
		link.setAttribute("href", "#");
		link.title = "delete";
		link.innerHTML = link.title;
		link.onclick = function(){removeDoctorFromTable(row);};
		
		cellRight.appendChild(link);
		
	}
function removeDoctorFromTable(row){
	var tbl = document.getElementById('doctorTable');
	tbl.deleteRow(row.rowIndex);
}
</script>


</head>
<body>
<jsp:include page="/navigation/navigation.jsp"/>

	<div id="mainBox">
		<center>
			<s:if test="actionMode=='create'">
				<h2>Create Patient </h2>
			</s:if>
			<s:else>
				<h2>Update Patient Information</h2>
			</s:else>
		</center>
		<center>
				<s:form name="editPatientForm" action="editPatient" method="post">
					<table width="300px">
						<tr>
							<td><img id = "userImage" src = "<s:url value="/resources/user.png" />" width=100px height=120px alt="404 Not Found"></td>
						</tr>
						
						<s:if test="actionMode!='create'">
							<tr>
								<td>Id:</td>
								<td><s:property value="retrievedPatient.id"/></td>
							</tr>
						</s:if>
						
						<s:if test="actionMode=='patientUpdate'">
							<tr>
								<td>Default Doctor:</td>
								<td><s:property value="defaultDoctor.getName()"/></td>
							</tr>
							<tr>
								<td>Username:</td>
								<td><s:property value="retrievedPatient.username"/></td>
							</tr>
							<tr>
								<td>OHIP:</td>
								<td><s:property value="retrievedPatient.ohip"/></td>
							</tr>
							<tr>
								<td>SIN:</td>
								<td><s:property value="retrievedPatient.sin"/></td>
							</tr>
							<s:textfield name="retrievedPatient.name" label="Name"/>
							<s:textfield name="retrievedPatient.phone" label="Phone"/>
							<s:textfield name="retrievedPatient.address" label="Address"/>
							<s:password name="retrievedPatient.password" label="Reset password"/>
							<tr><td colSpan="3">Ignore this field if you don't want to reset</td></tr>
						</s:if>
						
						<s:if test="actionMode=='doctorUpdate'">
							<tr>
								<td>Default Doctor:</td>
								<td><s:property value="defaultDoctor.getName()"/></td>
							</tr>
							<tr>
								<td>Patient name:</td>
								<td><s:property value="retrievedPatient.name"/></td>
							</tr>
							<tr>
								<td>Phone:</td>
								<td><s:property value="retrievedPatient.phone"/></td>
							</tr>
							<tr>
								<td>Address:</td>
								<td><s:property value="retrievedPatient.address"/></td>
							</tr>
							<tr>
								<td>OHIP:</td>
								<td><s:property value="retrievedPatient.ohip"/></td>
							</tr>
							<tr>
								<td>SIN:</td>
								<td><s:property value="retrievedPatient.sin"/></td>
							</tr>
							<tr>
								<td>Last Visit Date:</td>
								<td><s:property value="retrievedPatient.lastVisitTime"/></td>
							</tr>
							<s:textfield name="retrievedPatient.currentHealth" label="Current health"/>							
							<tr>
								<td valign="top">Read Only Doctors:</td>
								<td>
									<table id="doctorTable">
										<s:iterator value="retrievedReadOnlyDoctorIds" var="pa">
											<tr>
												<td>
													<select name="selectedReadOnlyDoctorIds">
														<s:iterator value="retrievedDoctors">
															<option value="<s:property value="id"/>"
																<s:if test="id==doctorId">selected = "selected"</s:if>>
																<s:property value="name" />
															</option>
														</s:iterator>
													</select>
												</td>
												<td><a href="#" onClick="removeDoctorFromTable(this.parentNode.parentNode)"> delete</a></td>
											</tr>
										</s:iterator>
									</table>
								</td>
							</tr>
							
							<tr><td><a href="#" onClick="addDoctorToTable()"> Add New Doctor</a></td></tr>
						</s:if>
						
						<s:if test="actionMode=='create'" >
						
							<s:textfield name="retrievedPatient.username" label="username"/>
							<s:password name="retrievedPatient.password" label="password"/>
							<s:textfield name="retrievedPatient.phone" label="Phone"/>
							<s:textfield name="retrievedPatient.address" label="Address"/>
							<s:textfield name="retrievedPatient.name" label="Name"/>
							<s:textfield name="retrievedPatient.ohip" label="OHIP"/>
							<s:textfield name="retrievedPatient.sin" label="SIN"/>
						
							<tr>
							<td>Default Doctor:</td>
							<td>
								<select name="selectedDefaultDoctorId" >
										<s:iterator value="retrievedDoctors">
								    		<option value="<s:property value="id"/>" ><s:property value="name"/></option>
										</s:iterator>
								</select>
							</td>
							</tr>
						</s:if>
						
						<s:if test="actionMode=='staffUpdate'">
						
							<tr><td>Username:</td><td><s:property value="retrievedPatient.username"/></td></tr>
							<s:textfield name="retrievedPatient.name" label="Name"/>
							<s:textfield name="retrievedPatient.phone" label="Phone"/>
							<s:textfield name="retrievedPatient.address" label="Address"/>
							<s:textfield name="retrievedPatient.ohip" label="OHIP"/>
							<s:textfield name="retrievedPatient.sin" label="SIN"/>
						
							<tr>
							<td>Default Doctor:</td>
							<td>
								<select name="selectedDefaultDoctorId" >
										<s:iterator value="retrievedDoctors">
								    		<option value="<s:property value="id"/>" 
								    		<s:if test="id==defaultDoctor.id">selected = "selected"</s:if>
								    		><s:property value="name"/></option>
										</s:iterator>
								</select>
							</td>
							</tr>
						</s:if>
						
						
						<tr>
						<s:hidden name="actionMode" value="%{actionMode}" />
						<s:hidden name="retrievedPatient.id" value="%{retrievedPatient.id}" />
						<s:if test="actionMode!='create'">
							<s:submit value="Update" method="updatePatientInfo"/>
						</s:if>
						<s:if test="actionMode=='create'">
							<s:hidden name="selectedEmployeeId" value="%{#session.currentUser.id}" />
							<s:submit value="Create" method="createPatient"/>
						</s:if>
						</tr>
					</table>
			</s:form>
			<div id="actionResult"><s:property value="#session.actionResult"/></div>
		</center>
		
	</div>
</body>
</html>